Sealed Classes and Interfaces
π‘οΈ Sealed Classes and Interfaces in Java 15 β Now With 100% More Sass ββ
Welcome, brave Java warrior! Today, we embark on a journey through the mysterious and powerful realm of sealed classes and interfacesβa realm where only the chosen few are allowed to inherit.
Before Java 15, your public interfaces were wide open like a coffee shop with no closing hours. Now? Weβre slapping on some velvet ropes and exclusive guest lists. π«π¨βπ»
1. π Sealed Classesβ
1.1 π sealed
Modifier and permits
Keywordβ
By default, Java classes are like open mic nightsβanyone can walk up and implement your public interface. But now with Java 15βs preview feature, you can seal the deal.
How? Just toss in the magical sealed
modifier, and declare your A-list subclasses using permits
. π«
sealed class Account
permits CurrentAccount, SavingAccount, LoanAccount {
}
final class CurrentAccount extends Account {}
non-sealed class SavingAccount extends Account {}
sealed class LoanAccount extends Account permits HomeloanAccount, AutoloanAccount {}
final class HomeloanAccount extends LoanAccount {}
final class AutoloanAccount extends LoanAccount {}
π Result? Only these 3 subclasses are allowed to extend Account
. No party crashers allowed!
π― And for LoanAccount
, only HomeloanAccount
and AutoloanAccount
get VIP access.
1.2 𧬠final
, sealed
, and non-sealed
Subclassesβ
Once you're on the guest list, how you behave matters:
final
: βIβm the last in my line. No one inherits from me. Period.β πsealed
: βYou can inherit, but only if youβre invited.β πnon-sealed
: βIβm chill. Anyone can extend me. Live your life.β π
π‘ Important: A sealed class cannot force its subclasses to keep the sealing tradition. Rebels gonna rebel.
1.3 π¦ Packaging of Sealed Classesβ
There are some location rules to keep your sealed family together:
- All permitted subclasses must be in the same module.
- If you're in the unnamed module, then they must be in the same package.
- Bonus hack: If you put all the subclasses in the same
.java
file, you can omit thepermits
keyword!
Example:
public sealed class Account {} // No 'permits' keyword needed
final class CurrentAccount extends Account {}
non-sealed class SavingAccount extends Account {}
sealed class LoanAccount extends Account {}
final class HomeloanAccount extends LoanAccount {}
final class AutoloanAccount extends LoanAccount {}
π§ Magical, isnβt it?
1.4 π Records as Sealed Subclassesβ
Guess what? You can totally add a record class to the permits
list! Because records are:
- Automatically
final
- Immutable
- Made for modeling data with β¨ style β¨
So go ahead and drop a record into your sealed class family. They wonβt ruin the party.
1.5 π§ͺ API Supportβ
Java's reflection API decided to get in on the action too. Say hello to:
Class::permittedSubclasses()
β Returns a nice little array of all the permitted subclasses.Class::isSealed()
β Returnstrue
if the class is sealed. π
Now you can seal and reflect at the same time. π
2. π€ Sealed Interfacesβ
Interfaces wanted in on the funβand Java said, βSure, why not?β
Same rules, just a different vibe. Add sealed
to your interface and use permits
to name the lucky few who can implement or extend it.
public sealed interface IReport
permits Printable, Formattable, ExcelReport, PdfReport {
}
non-sealed interface Printable extends IReport {}
non-sealed interface Formattable extends IReport {}
non-sealed class ExcelReport implements IReport {}
non-sealed class PdfReport implements IReport {}
π§Ύ Sealed interfaces follow the exact same declaration rules as sealed classes. No extra drama.
3. π§ When Should You Use Sealed Classes?β
You know that one time you wrote 10 instanceof
checks and STILL missed a subclass? Yeah... sealed classes are here to save you.
Why use them?
- Lock down class hierarchies like a boss.
- Prevent surprise subclassing from rogue developers.
- Help the compiler ensure you've handled all the cases in your
instanceof
checks.
Future versions of Java + pattern matching = no more default or else blocks. π
As soon as a new subclass joins the sealed class squad, the compiler taps you on the shoulder and says, βYo, you forgot to handle this one.β
** π Happy Learning!!
Thatβs the sealed class circus, folks! Use it to tame your type hierarchies, control subclassing chaos, and become the master of your Java destiny. π§ββοΈβ
Until next timeβkeep your classes sealed and your bugs revealed!